﻿package com.skcc.cop.mrtaxi.biz.destination;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.skcc.cop.mrtaxi.BaseActivity;
import com.skcc.cop.mrtaxi.R;
import com.skcc.cop.mrtaxi.biz.HomeListActivity;
import com.skcc.cop.mrtaxi.biz.HomeMapActivity;
import com.skcc.cop.mrtaxi.http.DestinationListener;
import com.skcc.cop.mrtaxi.http.model.Destination;
import com.skcc.cop.mrtaxi.http.model.Driver;
import com.skcc.cop.mrtaxi.util.Dialogs;
import com.skcc.cop.mrtaxi.util.LogControl;
import com.skcc.cop.mrtaxi.util.Dialogs.DialogListener;
import com.skcc.cop.mrtaxi.view.TitleBar;
import com.skcc.cop.mrtaxi.view.TitleBar.OnTitleBarEventListener;


public class ManageDestinationActivity extends BaseActivity implements OnItemClickListener, OnClickListener, OnItemLongClickListener, OnTitleBarEventListener{

	private static final String TAG = ManageDestinationActivity.class.getSimpleName();
	
	private ImageButton mBackImageButton, mAddImageButton;
	private ListView listView;
	private ArrayList<String> list;
	private ArrayAdapter<String> adapter;
	private ArrayList<DestinationEntity> destinationEntity;
	private HashMap<Integer, Integer> hMap;
	private TitleBar mTitleBarView;
	private DestinationListAdapter mAdapter;
	private List<Destination> DestinationList = new ArrayList<Destination> ();
	private ListView mDestinationList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_destination_manage);
		
		mTitleBarView = (TitleBar) this.findViewById(R.id.titlebar_view);
		mTitleBarView.setEventListener(this);
		mTitleBarView.showBack();
		mTitleBarView.showAdd();
		mTitleBarView.setTitle(R.string.destination_management);
		

//		mBackImageButton = (ImageButton)findViewById(R.id.destination_manage_back_imageButton);
//		mAddImageButton = (ImageButton)findViewById(R.id.destination_manage_add_imageButton);
//
//		//MrTaxiApplication MA = new MrTaxiApplication();
//
//		mBackImageButton.setOnClickListener(this);
//		mAddImageButton.setOnClickListener(this);
		
		mDestinationList = (ListView) this.findViewById(R.id.destination_listview);
		
		//listView = (ListView)findViewById(android.R.id.list);
		list = new ArrayList<String>();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		Log.i(TAG, "onResume()");
		super.onResume();
		
		review();
	}
	
	@Override
	public void onBackClicked() {
		Intent intent = new Intent(getBaseContext(),HomeListActivity.class);
        startActivity(intent);
		finish();
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(getBaseContext(),HomeListActivity.class);
        startActivity(intent);
		finish();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
//		if (v.getId() == R.id.destination_manage_back_imageButton){
//
//			Toast.makeText(ManageDestinationActivity.this, "뒤로", Toast.LENGTH_SHORT).show();
//			finish();
//
//
//		}else if (v.getId() == R.id.destination_manage_add_imageButton){
//
//			Toast.makeText(ManageDestinationActivity.this, "새로만들기", Toast.LENGTH_SHORT).show();
//			Intent intent = new Intent(ManageDestinationActivity.this, AddDestinationActivity.class);
//			startActivity(intent);
//		}
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View arg1, int position, long id) {
		// TODO Auto-generated method stub
		String ku = Integer.toString(position);

		Toast.makeText(ManageDestinationActivity.this, list.get(position) + "\n" + ku, Toast.LENGTH_SHORT).show();


		Intent intent = new Intent(ManageDestinationActivity.this, ModifyDestinationActivity.class);
		intent.putExtra("address", list.get(position));
		intent.putExtra("dbId", getMappingId(position));
		startActivity(intent);
		
		
/*		if(destinationEntity != null){
			//서버전송 Data set
			Destination des = new Destination();
			for(DestinationEntity entity : destinationEntity){
				Log.i(TAG, "entity.getId() = " + entity.getId());
				if(entity.getId() == getMappingId(position)){
					des.setPsgLatitude(entity.getLati());
					des.setPsgLongitude(entity.getLongi());
					des.setPsgAddr1(entity.getAddress1());
					des.setPsgNum(entity.getPassenger());
					des.setUserId("testID1");
					des.setDestRegtEdit("I");
					break;
				}
			}
			//서버전송
			if(des !=  null)
				RegisterDestReq(des);
		}*/
		


	}
	
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View arg1, final int position, long id){
		// TODO Auto-generated method stub
		Log.i(TAG, ""+position);
		Dialogs.showDialogYesNo(ManageDestinationActivity.this, getString(R.string.destination_delete_destination), new DialogListener() {

			@Override
			public void onClickedOk() {
				dataBaseControl.deleteDestination(getMappingId(position));
				list.remove(position);
				adapter.notifyDataSetChanged();
				Dialogs.dismiss();
			}

			@Override
			public void onClickedCancel() {
				Dialogs.dismiss();
			}
			
		});
		
		return true;
	}
	
	@SuppressLint("UseSparseArrays")
	private void review(){
		hMap = new HashMap<Integer, Integer>();
		
		int i = 0;
		//DB에서 목적지 List를 가져옴
		destinationEntity = dataBaseControl.getDestinationListAll();
		list.clear(); //onResume시 중복을 막기위해
		DestinationList.clear();
		//DestinationList<Destination> desList = new Destination<Destination>() ;
		for(DestinationEntity entity : destinationEntity){
			Log.i(TAG, "entity.getId() = " + entity.getId());
			Log.i(TAG, ""+entity.getAddress1());
			Log.i(TAG, ""+entity.getLati());
			Log.i(TAG, ""+entity.getLongi());
			
//			list.add(entity.getCity() + " " + entity.getDistrict() + " " + entity.getStreet() 
//					+ " : " + entity.getPassenger() + "명");	
			list.add(entity.getAddress1()+ " : " + entity.getPassenger() + "명");	
			hMap.put(i, entity.getId()); //ListView의 position과 DB id Mapping
			Destination des = new Destination();
			des.setPsgAddr1(entity.getAddress1());
			des.setPsgNum(entity.getPassenger());
			DestinationList.add(des);
			i++;
		}
		
		
		
		//adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
		//adapter = new ArrayAdapter<String>(this, R.layout.activity_destination_manage_list, list);
		mAdapter = new DestinationListAdapter(ManageDestinationActivity.this, R.layout.view_destination_manage_list, DestinationList);
		
		mDestinationList.setAdapter(mAdapter);
		
		//mDestinationList.setAdapter(adapter);
		mDestinationList.setOnItemClickListener(this);
		mDestinationList.setOnItemLongClickListener(this);
	}
	
	private int getMappingId(int idx){
		return hMap.get(idx);
	}
	
	private void RegisterDestReq(Destination des) {
		
		showProgressDialog(this, R.string.loading);
		
		gateway.RegisterDestination(des, new DestinationListener() {

			@Override
			public void onNoNetwork() {
				dismissProgressDialog();
				
				Dialogs.showDialog(ManageDestinationActivity.this, getString(R.string.no_network), dialogListener);
			}

			@Override
			public void onNoResponse() {
				dismissProgressDialog();
				
				Dialogs.showDialog(ManageDestinationActivity.this, getString(R.string.no_respond), dialogListener);
			}

			@Override
			public void onSessionTimeOut() {
				dismissProgressDialog();
				
				Dialogs.showDialog(ManageDestinationActivity.this, getString(R.string.session_timeout), dialogListener);
			}


			@Override
			public void onFail(int code, String msg) {
				dismissProgressDialog();
				
				Dialogs.showDialog(ManageDestinationActivity.this, getString(R.string.server_message) + msg, dialogListener);
			}

			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				Log.i(TAG, "DestRegisterReq Success");
				
			}
		});
	}
	
	private DialogListener dialogListener = new DialogListener() {

		@Override
		public void onClickedOk() {
			Dialogs.dismiss();
			
			finish();
		}

		@Override
		public void onClickedCancel() {
			// TODO Auto-generated method stub
		}
	};


	@Override
	public void onAddClicked() {
		// TODO Auto-generated method stub
		
		Toast.makeText(ManageDestinationActivity.this, "새로만들기", Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(ManageDestinationActivity.this, AddDestinationActivity.class);
		startActivity(intent);
		finish();
	}
	
	class DestinationListAdapter extends ArrayAdapter<Destination> {
		
		private final LayoutInflater inflater;
		private final int resource;

		public DestinationListAdapter(Context context, int resId, List<Destination> items) {
			super(context, resId, items);
			this.resource = resId;
			this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Log.i(TAG, "position = " + position);
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(resource, parent, false);
				holder = new ViewHolder();
				holder.address = (TextView) convertView.findViewById(R.id.destination_address);
				holder.passenger = (TextView) convertView.findViewById(R.id.destination_passenger);
				holder.destinationIcn = (ImageView)convertView.findViewById(R.id.destination_icn);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Destination item = getItem(position);
			holder.address.setText(item.getPsgAddr1());
			holder.passenger.setText(" X "+item.getPsgNum());
			if(position == 0)
				holder.destinationIcn.setBackgroundResource(R.drawable.destination_on);
			
			return convertView;
		}
		class ViewHolder {

			TextView address; 
			TextView passenger;	
			ImageView destinationIcn;
			
		}
	}
	

}